<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>Chapter 3 Chapter 3 | Data visualisation using R, for researchers who don’t use R</title>
  <meta name="description" content="Abstract here." />
  <meta name="generator" content="bookdown 0.22 and GitBook 2.6.7" />

  <meta property="og:title" content="Chapter 3 Chapter 3 | Data visualisation using R, for researchers who don’t use R" />
  <meta property="og:type" content="book" />
  
  
  <meta property="og:description" content="Abstract here." />
  

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="Chapter 3 Chapter 3 | Data visualisation using R, for researchers who don’t use R" />
  
  <meta name="twitter:description" content="Abstract here." />
  

<meta name="author" content="Emily Nordmann, Phil McAleer, Wilhelmiina Toivo, Helena Paterson, Lisa DeBruine" />


<meta name="date" content="2021-06-17" />

  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="chapter-2.html"/>
<link rel="next" href="chapter-4.html"/>
<script src="libs/header-attrs-2.9/header-attrs.js"></script>
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />









<link href="libs/anchor-sections-1.0.1/anchor-sections.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.0.1/anchor-sections.js"></script>
<meta property="og:title" content="IntroDataViz">
<meta property="og:description" content="Data Visualisation Using R, For Researchers Who Don’t Use R.">
<meta property="og:image" content="https://psyteachr.github.io/images/twitter/psyteachr.png">
<meta property="og:url" content="https://psyteachr.github.io/introdataviz/">
<meta property="twitter:card" content="summary_large_image">

<link rel="shortcut icon" href="https://psyteachr.github.io/images/logo.png">


<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
  { counter-reset: source-line 0; }
pre.numberSource code > span
  { position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
  { content: counter(source-line);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {   }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<style type="text/css">
/* Used with Pandoc 2.11+ new --citeproc when CSL is used */
div.csl-bib-body { }
div.csl-entry {
  clear: both;
}
.hanging div.csl-entry {
  margin-left:2em;
  text-indent:-2em;
}
div.csl-left-margin {
  min-width:2em;
  float:left;
}
div.csl-right-inline {
  margin-left:2em;
  padding-left:1em;
}
div.csl-indent {
  margin-left: 2em;
}
</style>

<link rel="stylesheet" href="include/psyteachr.css" type="text/css" />
<link rel="stylesheet" href="include/webex.css" type="text/css" />
<link rel="stylesheet" href="include/style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">IntroDataViz</a></li>
<li><a href="https://zenodo.org/badge/latestdoi/166559207"><img src="https://zenodo.org/badge/166559207.svg" alt="DOI"></a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Overview</a></li>
<li class="chapter" data-level="1" data-path="introduction.html"><a href="introduction.html"><i class="fa fa-check"></i><b>1</b> Introduction</a>
<ul>
<li class="chapter" data-level="1.1" data-path="introduction.html"><a href="introduction.html#why-r-for-data-visualisation"><i class="fa fa-check"></i><b>1.1</b> Why R for data visualisation?</a></li>
<li class="chapter" data-level="1.2" data-path="introduction.html"><a href="introduction.html#a-layered-grammar-of-graphics"><i class="fa fa-check"></i><b>1.2</b> A layered grammar of graphics</a></li>
<li class="chapter" data-level="1.3" data-path="introduction.html"><a href="introduction.html#simulated-dataset"><i class="fa fa-check"></i><b>1.3</b> Simulated dataset</a></li>
<li class="chapter" data-level="1.4" data-path="introduction.html"><a href="introduction.html#setting-up-r-and-rstudio"><i class="fa fa-check"></i><b>1.4</b> Setting up R and RStudio</a></li>
<li class="chapter" data-level="1.5" data-path="introduction.html"><a href="introduction.html#preparing-your-data"><i class="fa fa-check"></i><b>1.5</b> Preparing your data</a>
<ul>
<li class="chapter" data-level="1.5.1" data-path="introduction.html"><a href="introduction.html#data-format"><i class="fa fa-check"></i><b>1.5.1</b> Data format</a></li>
<li class="chapter" data-level="1.5.2" data-path="introduction.html"><a href="introduction.html#variable-names"><i class="fa fa-check"></i><b>1.5.2</b> Variable names</a></li>
<li class="chapter" data-level="1.5.3" data-path="introduction.html"><a href="introduction.html#data-values"><i class="fa fa-check"></i><b>1.5.3</b> Data values</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="chapter-2.html"><a href="chapter-2.html"><i class="fa fa-check"></i><b>2</b> Chapter 2</a>
<ul>
<li class="chapter" data-level="2.1" data-path="chapter-2.html"><a href="chapter-2.html#loading-packages"><i class="fa fa-check"></i><b>2.1</b> Loading packages</a></li>
<li class="chapter" data-level="2.2" data-path="chapter-2.html"><a href="chapter-2.html#loading-data"><i class="fa fa-check"></i><b>2.2</b> Loading data</a></li>
<li class="chapter" data-level="2.3" data-path="chapter-2.html"><a href="chapter-2.html#handling-numeric-factors"><i class="fa fa-check"></i><b>2.3</b> Handling numeric factors</a></li>
<li class="chapter" data-level="2.4" data-path="chapter-2.html"><a href="chapter-2.html#argument-names"><i class="fa fa-check"></i><b>2.4</b> Argument names</a></li>
<li class="chapter" data-level="2.5" data-path="chapter-2.html"><a href="chapter-2.html#demographic-information"><i class="fa fa-check"></i><b>2.5</b> Demographic information</a></li>
<li class="chapter" data-level="2.6" data-path="chapter-2.html"><a href="chapter-2.html#bar-chart-of-counts"><i class="fa fa-check"></i><b>2.6</b> Bar chart of counts</a></li>
<li class="chapter" data-level="2.7" data-path="chapter-2.html"><a href="chapter-2.html#plotting-existing-aggregates-and-percent"><i class="fa fa-check"></i><b>2.7</b> Plotting existing aggregates and percent</a></li>
<li class="chapter" data-level="2.8" data-path="chapter-2.html"><a href="chapter-2.html#histogram"><i class="fa fa-check"></i><b>2.8</b> Histogram</a></li>
<li class="chapter" data-level="2.9" data-path="chapter-2.html"><a href="chapter-2.html#customisation-1"><i class="fa fa-check"></i><b>2.9</b> Customisation 1</a>
<ul>
<li class="chapter" data-level="2.9.1" data-path="chapter-2.html"><a href="chapter-2.html#changing-colours"><i class="fa fa-check"></i><b>2.9.1</b> Changing colours</a></li>
<li class="chapter" data-level="2.9.2" data-path="chapter-2.html"><a href="chapter-2.html#editing-axis-names-and-labels"><i class="fa fa-check"></i><b>2.9.2</b> Editing axis names and labels</a></li>
<li class="chapter" data-level="2.9.3" data-path="chapter-2.html"><a href="chapter-2.html#discrete-vs.-continuous-errors"><i class="fa fa-check"></i><b>2.9.3</b> Discrete vs. continuous errors</a></li>
<li class="chapter" data-level="2.9.4" data-path="chapter-2.html"><a href="chapter-2.html#adding-a-theme"><i class="fa fa-check"></i><b>2.9.4</b> Adding a theme</a></li>
</ul></li>
<li class="chapter" data-level="2.10" data-path="chapter-2.html"><a href="chapter-2.html#activities-1"><i class="fa fa-check"></i><b>2.10</b> Activities 1</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="chapter-3.html"><a href="chapter-3.html"><i class="fa fa-check"></i><b>3</b> Chapter 3</a>
<ul>
<li class="chapter" data-level="3.1" data-path="chapter-3.html"><a href="chapter-3.html#data-formats"><i class="fa fa-check"></i><b>3.1</b> Data formats</a></li>
<li class="chapter" data-level="3.2" data-path="chapter-3.html"><a href="chapter-3.html#transforming-data"><i class="fa fa-check"></i><b>3.2</b> Transforming data</a>
<ul>
<li class="chapter" data-level="3.2.1" data-path="chapter-3.html"><a href="chapter-3.html#step-1-pivot_longer"><i class="fa fa-check"></i><b>3.2.1</b> Step 1: <code>pivot_longer()</code></a></li>
<li class="chapter" data-level="3.2.2" data-path="chapter-3.html"><a href="chapter-3.html#step-2-pivot_longer-adjusted"><i class="fa fa-check"></i><b>3.2.2</b> Step 2: <code>pivot_longer()</code> adjusted</a></li>
<li class="chapter" data-level="3.2.3" data-path="chapter-3.html"><a href="chapter-3.html#step-3-pivot_wider"><i class="fa fa-check"></i><b>3.2.3</b> Step 3: <code>pivot_wider()</code></a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="chapter-3.html"><a href="chapter-3.html#histogram-2"><i class="fa fa-check"></i><b>3.3</b> Histogram 2</a></li>
<li class="chapter" data-level="3.4" data-path="chapter-3.html"><a href="chapter-3.html#density-plots"><i class="fa fa-check"></i><b>3.4</b> Density plots</a>
<ul>
<li class="chapter" data-level="3.4.1" data-path="chapter-3.html"><a href="chapter-3.html#grouped-density-plots"><i class="fa fa-check"></i><b>3.4.1</b> Grouped density plots</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="chapter-3.html"><a href="chapter-3.html#scatterplots"><i class="fa fa-check"></i><b>3.5</b> Scatterplots</a>
<ul>
<li class="chapter" data-level="3.5.1" data-path="chapter-3.html"><a href="chapter-3.html#grouped-scatterplots"><i class="fa fa-check"></i><b>3.5.1</b> Grouped scatterplots</a></li>
</ul></li>
<li class="chapter" data-level="3.6" data-path="chapter-3.html"><a href="chapter-3.html#transforming-data-2"><i class="fa fa-check"></i><b>3.6</b> Transforming data 2</a></li>
<li class="chapter" data-level="3.7" data-path="chapter-3.html"><a href="chapter-3.html#customisation-2"><i class="fa fa-check"></i><b>3.7</b> Customisation 2</a>
<ul>
<li class="chapter" data-level="3.7.1" data-path="chapter-3.html"><a href="chapter-3.html#accessible-colour-schemes"><i class="fa fa-check"></i><b>3.7.1</b> Accessible colour schemes</a></li>
</ul></li>
<li class="chapter" data-level="3.8" data-path="chapter-3.html"><a href="chapter-3.html#activities-2"><i class="fa fa-check"></i><b>3.8</b> Activities 2</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="chapter-4.html"><a href="chapter-4.html"><i class="fa fa-check"></i><b>4</b> Chapter 4</a>
<ul>
<li class="chapter" data-level="4.1" data-path="chapter-4.html"><a href="chapter-4.html#boxplots"><i class="fa fa-check"></i><b>4.1</b> Boxplots</a>
<ul>
<li class="chapter" data-level="4.1.1" data-path="chapter-4.html"><a href="chapter-4.html#grouped-boxplots"><i class="fa fa-check"></i><b>4.1.1</b> Grouped boxplots</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="chapter-4.html"><a href="chapter-4.html#violin-plots"><i class="fa fa-check"></i><b>4.2</b> Violin plots</a></li>
<li class="chapter" data-level="4.3" data-path="chapter-4.html"><a href="chapter-4.html#bar-chart-of-means"><i class="fa fa-check"></i><b>4.3</b> Bar chart of means</a></li>
<li class="chapter" data-level="4.4" data-path="chapter-4.html"><a href="chapter-4.html#violin-boxplot"><i class="fa fa-check"></i><b>4.4</b> Violin-boxplot</a>
<ul>
<li class="chapter" data-level="4.4.1" data-path="chapter-4.html"><a href="chapter-4.html#grouped-violin-boxplots"><i class="fa fa-check"></i><b>4.4.1</b> Grouped violin-boxplots</a></li>
</ul></li>
<li class="chapter" data-level="4.5" data-path="chapter-4.html"><a href="chapter-4.html#customisation-part-3"><i class="fa fa-check"></i><b>4.5</b> Customisation part 3</a></li>
<li class="chapter" data-level="4.6" data-path="chapter-4.html"><a href="chapter-4.html#activities-3"><i class="fa fa-check"></i><b>4.6</b> Activities 3</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="chapter-5.html"><a href="chapter-5.html"><i class="fa fa-check"></i><b>5</b> Chapter 5</a>
<ul>
<li class="chapter" data-level="5.1" data-path="chapter-5.html"><a href="chapter-5.html#interaction-plots"><i class="fa fa-check"></i><b>5.1</b> Interaction plots</a></li>
<li class="chapter" data-level="5.2" data-path="chapter-5.html"><a href="chapter-5.html#combined-interaction-plots"><i class="fa fa-check"></i><b>5.2</b> Combined interaction plots</a></li>
<li class="chapter" data-level="5.3" data-path="chapter-5.html"><a href="chapter-5.html#facets"><i class="fa fa-check"></i><b>5.3</b> Facets</a></li>
<li class="chapter" data-level="5.4" data-path="chapter-5.html"><a href="chapter-5.html#saving-plots"><i class="fa fa-check"></i><b>5.4</b> Saving plots</a></li>
<li class="chapter" data-level="5.5" data-path="chapter-5.html"><a href="chapter-5.html#exporting-plots"><i class="fa fa-check"></i><b>5.5</b> Exporting plots</a></li>
<li class="chapter" data-level="5.6" data-path="chapter-5.html"><a href="chapter-5.html#multiple-plots"><i class="fa fa-check"></i><b>5.6</b> Multiple plots</a>
<ul>
<li class="chapter" data-level="5.6.1" data-path="chapter-5.html"><a href="chapter-5.html#combining-two-plots"><i class="fa fa-check"></i><b>5.6.1</b> Combining two plots</a></li>
<li class="chapter" data-level="5.6.2" data-path="chapter-5.html"><a href="chapter-5.html#combining-three-or-more-plots"><i class="fa fa-check"></i><b>5.6.2</b> Combining three or more plots</a></li>
</ul></li>
<li class="chapter" data-level="5.7" data-path="chapter-5.html"><a href="chapter-5.html#customisation-part-4"><i class="fa fa-check"></i><b>5.7</b> Customisation part 4</a>
<ul>
<li class="chapter" data-level="5.7.1" data-path="chapter-5.html"><a href="chapter-5.html#axis-labels"><i class="fa fa-check"></i><b>5.7.1</b> Axis labels</a></li>
<li class="chapter" data-level="5.7.2" data-path="chapter-5.html"><a href="chapter-5.html#non-meaningful-colours"><i class="fa fa-check"></i><b>5.7.2</b> Non-meaningful colours</a></li>
</ul></li>
<li class="chapter" data-level="5.8" data-path="chapter-5.html"><a href="chapter-5.html#activities-4"><i class="fa fa-check"></i><b>5.8</b> Activities 4</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="chapter-6.html"><a href="chapter-6.html"><i class="fa fa-check"></i><b>6</b> Chapter 6</a>
<ul>
<li class="chapter" data-level="6.0.1" data-path="chapter-6.html"><a href="chapter-6.html#split-violin-plots"><i class="fa fa-check"></i><b>6.0.1</b> Split-violin plots</a></li>
<li class="chapter" data-level="6.0.2" data-path="chapter-6.html"><a href="chapter-6.html#raincloud-plots"><i class="fa fa-check"></i><b>6.0.2</b> Raincloud plots</a></li>
<li class="chapter" data-level="6.0.3" data-path="chapter-6.html"><a href="chapter-6.html#ridge-plots"><i class="fa fa-check"></i><b>6.0.3</b> Ridge plots</a></li>
<li class="chapter" data-level="6.0.4" data-path="chapter-6.html"><a href="chapter-6.html#alluvial-plots"><i class="fa fa-check"></i><b>6.0.4</b> Alluvial plots</a></li>
</ul></li>
<li class="appendix"><span><b>Appendices</b></span></li>
<li class="chapter" data-level="A" data-path="additional-resources.html"><a href="additional-resources.html"><i class="fa fa-check"></i><b>A</b> Additional resources</a></li>
<li class="chapter" data-level="B" data-path="additional-advanced-plots-and-customisation-options.html"><a href="additional-advanced-plots-and-customisation-options.html"><i class="fa fa-check"></i><b>B</b> Additional advanced plots and customisation options</a>
<ul>
<li class="chapter" data-level="B.1" data-path="additional-advanced-plots-and-customisation-options.html"><a href="additional-advanced-plots-and-customisation-options.html#adding-lines-to-plots"><i class="fa fa-check"></i><b>B.1</b> Adding lines to plots</a></li>
<li class="chapter" data-level="B.2" data-path="additional-advanced-plots-and-customisation-options.html"><a href="additional-advanced-plots-and-customisation-options.html#zooming-in-and-out"><i class="fa fa-check"></i><b>B.2</b> Zooming in and out</a></li>
<li class="chapter" data-level="B.3" data-path="additional-advanced-plots-and-customisation-options.html"><a href="additional-advanced-plots-and-customisation-options.html#setting-the-axis-values"><i class="fa fa-check"></i><b>B.3</b> Setting the axis values</a></li>
<li class="chapter" data-level="B.4" data-path="additional-advanced-plots-and-customisation-options.html"><a href="additional-advanced-plots-and-customisation-options.html#controlling-the-legend"><i class="fa fa-check"></i><b>B.4</b> Controlling the Legend</a></li>
<li class="chapter" data-level="B.5" data-path="additional-advanced-plots-and-customisation-options.html"><a href="additional-advanced-plots-and-customisation-options.html#setting-a-lab-theme-using-theme"><i class="fa fa-check"></i><b>B.5</b> Setting A Lab Theme using <code>theme()</code></a></li>
<li class="chapter" data-level="B.6" data-path="additional-advanced-plots-and-customisation-options.html"><a href="additional-advanced-plots-and-customisation-options.html#easter-egg---overlaying-plots"><i class="fa fa-check"></i><b>B.6</b> Easter Egg - Overlaying Plots</a></li>
<li class="chapter" data-level="B.7" data-path="additional-advanced-plots-and-customisation-options.html"><a href="additional-advanced-plots-and-customisation-options.html#easter-egg---a-dumbbell-plot"><i class="fa fa-check"></i><b>B.7</b> Easter Egg - A Dumbbell Plot</a></li>
<li class="chapter" data-level="B.8" data-path="additional-advanced-plots-and-customisation-options.html"><a href="additional-advanced-plots-and-customisation-options.html#easter-egg---a-pie-chart"><i class="fa fa-check"></i><b>B.8</b> Easter Egg - A Pie Chart</a></li>
</ul></li>
<li class="divider"></li>
<li><a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/" 
    target="blank"><img alt="Creative Commons License" 
    style="border-width:0" 
    src="https://i.creativecommons.org/l/by-sa/4.0/88x31.png" /></a></li>
<li><a href="https://psyteachr.github.io/books" target="blank">PsyTeachR Books</a></li>

</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Data visualisation using R, for researchers who don’t use R</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="chapter-3" class="section level1" number="3">
<h1><span class="header-section-number">Chapter 3</span> Chapter 3</h1>
<div id="data-formats" class="section level2" number="3.1">
<h2><span class="header-section-number">3.1</span> Data formats</h2>
<p>To visualise the experimental reaction time and accuracy data using <code>ggplot</code>, we first need to reshape the data from wide-format to long-format and it is this step that can cause friction with novice users of R. Traditionally, psychologists have been taught data skills using wide-format data. Wide-format data typically has one row of data for each participant with separate columns for each score or variable. Where there are repeated-measures variables, the dependent variable is split across different columns with one measurement for each condition and where there is between groups variables, a separate column is added to encode the group to which a participant or observation belongs.</p>
<p>The simulated lexical decision data is currently in wide-format (see Table <a href="chapter-3.html#tab:wide-data">3.1</a>) where each participant’s aggregated<a href="#fn3" class="footnote-ref" id="fnref3"><sup>3</sup></a> reaction time and accuracy for each level of the within-subject variable is split across multiple columns.</p>
<table>
<caption><span id="tab:wide-data">Table 3.1: </span>Data in wide format.</caption>
<thead>
<tr class="header">
<th align="left">id</th>
<th align="right">age</th>
<th align="left">language</th>
<th align="right">rt_word</th>
<th align="right">rt_nonword</th>
<th align="right">acc_word</th>
<th align="right">acc_nonword</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">S001</td>
<td align="right">22</td>
<td align="left">monolingual</td>
<td align="right">379.46</td>
<td align="right">516.82</td>
<td align="right">99</td>
<td align="right">90</td>
</tr>
<tr class="even">
<td align="left">S002</td>
<td align="right">33</td>
<td align="left">monolingual</td>
<td align="right">312.45</td>
<td align="right">435.04</td>
<td align="right">94</td>
<td align="right">82</td>
</tr>
<tr class="odd">
<td align="left">S003</td>
<td align="right">23</td>
<td align="left">monolingual</td>
<td align="right">404.94</td>
<td align="right">458.50</td>
<td align="right">96</td>
<td align="right">87</td>
</tr>
<tr class="even">
<td align="left">S004</td>
<td align="right">28</td>
<td align="left">monolingual</td>
<td align="right">298.37</td>
<td align="right">335.89</td>
<td align="right">92</td>
<td align="right">76</td>
</tr>
<tr class="odd">
<td align="left">S005</td>
<td align="right">26</td>
<td align="left">monolingual</td>
<td align="right">316.42</td>
<td align="right">401.32</td>
<td align="right">91</td>
<td align="right">83</td>
</tr>
<tr class="even">
<td align="left">S006</td>
<td align="right">29</td>
<td align="left">monolingual</td>
<td align="right">357.17</td>
<td align="right">367.34</td>
<td align="right">96</td>
<td align="right">78</td>
</tr>
</tbody>
</table>
<p>Wide-format is popular because it is intuitive to read and easy to enter data into as all the data for one participant is contained within a single row. However, for the purposes of analysis, and particularly for analysis using R, this format is unsuitable. Whilst it is intuitive to read by a human, the same is not true for a computer. Wide-format data concatenates multiple pieces of information in a single column, for example in Table <a href="chapter-3.html#tab:wide-data">3.1</a>, <code>rt_word</code> contains information related to both a DV and one level of an IV. In comparison, long-format data separates the DV from the IV’s so that each column represents only one variable. The less intuitive part is that long data has multiple rows for each participant and a column that encodes the level of the IV (<code>word</code> or <code>nonword</code>). In essence, the long-format encodes repeated-measures variable in the same way as a between-group variable in SPSS. Wickham <span class="citation">(<a href="#ref-wickham2014tidy" role="doc-biblioref">2014</a>)</span> provides a comprehensive overview of the benefits of a similar format known as tidy data, which is a standard way of mapping a dataset to its structure, but for the purposes of this tutorial there are two important rules: each column should be a <em>variable</em> and each row should be an <em>observation.</em></p>
<p>Moving from using wide-form to long-form datasets can require a conceptual shift on the part of the researcher and one that usually only comes with practice and repeated exposure<a href="#fn4" class="footnote-ref" id="fnref4"><sup>4</sup></a>. For our example dataset, adhering to these rules for reshaping the data would produce Table <a href="chapter-3.html#tab:long">3.2</a>. Rather than different observations of the same dependent variable being split across columns, there is now a single column for the DV reaction time, and a single column for the DV accuracy. Each participant now has multiple rows of data, one for each observation (i.e., for each participant there will be as many rows as there are levels of the within-subject IV). Although there is some repetition of age and language group, each row is unique when looking at the combination of measures.</p>
<table>
<caption><span id="tab:long">Table 3.2: </span>Data in the correct format for visualization.</caption>
<thead>
<tr class="header">
<th align="left">id</th>
<th align="right">age</th>
<th align="left">language</th>
<th align="left">condition</th>
<th align="right">rt</th>
<th align="right">acc</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">S001</td>
<td align="right">22</td>
<td align="left">monolingual</td>
<td align="left">word</td>
<td align="right">379.46</td>
<td align="right">99</td>
</tr>
<tr class="even">
<td align="left">S001</td>
<td align="right">22</td>
<td align="left">monolingual</td>
<td align="left">nonword</td>
<td align="right">516.82</td>
<td align="right">90</td>
</tr>
<tr class="odd">
<td align="left">S002</td>
<td align="right">33</td>
<td align="left">monolingual</td>
<td align="left">word</td>
<td align="right">312.45</td>
<td align="right">94</td>
</tr>
<tr class="even">
<td align="left">S002</td>
<td align="right">33</td>
<td align="left">monolingual</td>
<td align="left">nonword</td>
<td align="right">435.04</td>
<td align="right">82</td>
</tr>
<tr class="odd">
<td align="left">S003</td>
<td align="right">23</td>
<td align="left">monolingual</td>
<td align="left">word</td>
<td align="right">404.94</td>
<td align="right">96</td>
</tr>
<tr class="even">
<td align="left">S003</td>
<td align="right">23</td>
<td align="left">monolingual</td>
<td align="left">nonword</td>
<td align="right">458.50</td>
<td align="right">87</td>
</tr>
</tbody>
</table>
<p>The benefits and flexibility of this format will hopefully become apparent as we progress through the tutorial, however, a useful rule of thumb when working with data in R for visualisation is that <em>anything that shares an axis should probably be in the same column</em>. For example, a simple bar chart of means for the reaction time DV would display the variable <code>condition</code> on the x-axis with bars representing both the <code>word</code> and <code>nonword</code> data, therefore, these data should be in one column and not split.</p>
</div>
<div id="transforming-data" class="section level2" number="3.2">
<h2><span class="header-section-number">3.2</span> Transforming data</h2>
<p>We have chosen a 2 x 2 design with two DVs as we anticipate that this is a design many researchers will be familiar with and may also have existing datasets with a similar structure. However, it is worth normalising that trial-and-error is part of the process of learning how to apply these functions to new datasets and structures. Data visualisation can be a useful way to scaffold learning these data transformations because they can provide a concrete visual check as to whether you have done what you intended to do with your data.</p>
<div id="step-1-pivot_longer" class="section level3" number="3.2.1">
<h3><span class="header-section-number">3.2.1</span> Step 1: <code>pivot_longer()</code></h3>
<p>The first step is to use the function <code>pivot_longer()</code> to transform the data to long-form. We have purposefully used a more complex dataset with two DVs for this tutorial to aid researchers applying our code to their own datasets. Because of this, we will break down the steps involved to help show how the code works.</p>
<p>This first code ignores that the dataset has two DVs, a problem we will fix in step 2. The pivot functions can be easier to show than tell - you may find it a useful exercise to run the below code and compare the newly created object <code>long</code> (Table <a href="chapter-3.html#tab:long1-example">3.3</a>) with the original <code>dat</code> Table <a href="chapter-3.html#tab:wide-data">3.1</a> before reading on.</p>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="chapter-3.html#cb25-1" aria-hidden="true" tabindex="-1"></a>long <span class="ot">&lt;-</span> <span class="fu">pivot_longer</span>(<span class="at">data =</span> dat, </span>
<span id="cb25-2"><a href="chapter-3.html#cb25-2" aria-hidden="true" tabindex="-1"></a>                     <span class="at">cols =</span> rt_word<span class="sc">:</span>acc_nonword, </span>
<span id="cb25-3"><a href="chapter-3.html#cb25-3" aria-hidden="true" tabindex="-1"></a>                     <span class="at">names_to =</span> <span class="st">&quot;dv_condition&quot;</span>,</span>
<span id="cb25-4"><a href="chapter-3.html#cb25-4" aria-hidden="true" tabindex="-1"></a>                     <span class="at">values_to =</span> <span class="st">&quot;dv&quot;</span>)</span></code></pre></div>
<ul>
<li><p>As with the other tidyverse functions, the first argument specifies the dataset to use as the base, in this case <code>dat</code>. This argument name is often dropped in examples.</p></li>
<li><p><code>cols</code> specifies all the columns you want to transform. The easiest way to visualise this is to think about which columns would be the same in the new long-form dataset and which will change. If you refer back to Table <a href="chapter-3.html#tab:wide-data">3.1</a>, you can see that <code>id</code>, <code>age</code>, and <code>language</code> all remain, while the columns that contain the measurements of the DVs change. The colon notation <code>first_column:last_column</code> is used to select all variables from the first column specified to the second. In our code, <code>cols</code> specifies that the columns we want to transform are <code>rt_word</code> to <code>acc_nonword</code>.</p></li>
<li><p><code>names_to</code> specifies the name of the new column that will be created.</p></li>
<li><p>Finally, <code>values_to</code> names the new column that will contain the measurements, in this case we’ll call it <code>dv</code>. At this point you may find it helpful to go back and compare <code>dat</code> and <code>long</code> again to see how each argument matches up with the output of the table.</p></li>
</ul>
<table>
<caption><span id="tab:long1-example">Table 3.3: </span>Data in long format with mixed DVs.</caption>
<thead>
<tr class="header">
<th align="left">id</th>
<th align="right">age</th>
<th align="left">language</th>
<th align="left">dv_condition</th>
<th align="right">dv</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">S001</td>
<td align="right">22</td>
<td align="left">monolingual</td>
<td align="left">rt_word</td>
<td align="right">379.46</td>
</tr>
<tr class="even">
<td align="left">S001</td>
<td align="right">22</td>
<td align="left">monolingual</td>
<td align="left">rt_nonword</td>
<td align="right">516.82</td>
</tr>
<tr class="odd">
<td align="left">S001</td>
<td align="right">22</td>
<td align="left">monolingual</td>
<td align="left">acc_word</td>
<td align="right">99.00</td>
</tr>
<tr class="even">
<td align="left">S001</td>
<td align="right">22</td>
<td align="left">monolingual</td>
<td align="left">acc_nonword</td>
<td align="right">90.00</td>
</tr>
<tr class="odd">
<td align="left">S002</td>
<td align="right">33</td>
<td align="left">monolingual</td>
<td align="left">rt_word</td>
<td align="right">312.45</td>
</tr>
<tr class="even">
<td align="left">S002</td>
<td align="right">33</td>
<td align="left">monolingual</td>
<td align="left">rt_nonword</td>
<td align="right">435.04</td>
</tr>
</tbody>
</table>
</div>
<div id="step-2-pivot_longer-adjusted" class="section level3" number="3.2.2">
<h3><span class="header-section-number">3.2.2</span> Step 2: <code>pivot_longer()</code> adjusted</h3>
<p>The problem with the above long-form data-set is that because we have ignored that there are two DVs, <code>dv_condition</code> still continues to conflate two variables - it has information about the type of DV and the condition of the IV. To account for this, we include a new argument <code>names_sep</code> and adjust <code>name_to</code> to specify the creation of two new columns. Note that we are pivoting the same wide-format dataset <code>dat</code> as we did in step 1.</p>
<ul>
<li><p><code>names_sep</code> specifies how to split up the variable name in cases where it has multiple components. This is when taking care to name your variables consistently and meaningfully pays off. Because the word to the left of the separator (<code>_</code>) is always the DV type and the word to the right is always the condition of the within-subject IV, it is easy to automatically split the columns.</p></li>
<li><p>Note that when specifying more than one column name, they must be combined using <code>c()</code> and be enclosed in their own quotation marks.</p></li>
</ul>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb26-1"><a href="chapter-3.html#cb26-1" aria-hidden="true" tabindex="-1"></a>long2 <span class="ot">&lt;-</span> <span class="fu">pivot_longer</span>(<span class="at">data =</span> dat, </span>
<span id="cb26-2"><a href="chapter-3.html#cb26-2" aria-hidden="true" tabindex="-1"></a>                     <span class="at">cols =</span> rt_word<span class="sc">:</span>acc_nonword, </span>
<span id="cb26-3"><a href="chapter-3.html#cb26-3" aria-hidden="true" tabindex="-1"></a>                     <span class="at">names_sep =</span> <span class="st">&quot;_&quot;</span>, </span>
<span id="cb26-4"><a href="chapter-3.html#cb26-4" aria-hidden="true" tabindex="-1"></a>                     <span class="at">names_to =</span> <span class="fu">c</span>(<span class="st">&quot;dv_type&quot;</span>, <span class="st">&quot;condition&quot;</span>),</span>
<span id="cb26-5"><a href="chapter-3.html#cb26-5" aria-hidden="true" tabindex="-1"></a>                     <span class="at">values_to =</span> <span class="st">&quot;dv&quot;</span>)</span></code></pre></div>
<table>
<caption><span id="tab:long-example">Table 3.4: </span>Data in long format.</caption>
<thead>
<tr class="header">
<th align="left">id</th>
<th align="right">age</th>
<th align="left">language</th>
<th align="left">dv_type</th>
<th align="left">condition</th>
<th align="right">dv</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">S001</td>
<td align="right">22</td>
<td align="left">monolingual</td>
<td align="left">rt</td>
<td align="left">word</td>
<td align="right">379.46</td>
</tr>
<tr class="even">
<td align="left">S001</td>
<td align="right">22</td>
<td align="left">monolingual</td>
<td align="left">rt</td>
<td align="left">nonword</td>
<td align="right">516.82</td>
</tr>
<tr class="odd">
<td align="left">S001</td>
<td align="right">22</td>
<td align="left">monolingual</td>
<td align="left">acc</td>
<td align="left">word</td>
<td align="right">99.00</td>
</tr>
<tr class="even">
<td align="left">S001</td>
<td align="right">22</td>
<td align="left">monolingual</td>
<td align="left">acc</td>
<td align="left">nonword</td>
<td align="right">90.00</td>
</tr>
<tr class="odd">
<td align="left">S002</td>
<td align="right">33</td>
<td align="left">monolingual</td>
<td align="left">rt</td>
<td align="left">word</td>
<td align="right">312.45</td>
</tr>
<tr class="even">
<td align="left">S002</td>
<td align="right">33</td>
<td align="left">monolingual</td>
<td align="left">rt</td>
<td align="left">nonword</td>
<td align="right">435.04</td>
</tr>
</tbody>
</table>
</div>
<div id="step-3-pivot_wider" class="section level3" number="3.2.3">
<h3><span class="header-section-number">3.2.3</span> Step 3: <code>pivot_wider()</code></h3>
<p>Although we have now split the columns so that there are separate variables for the DV type and level of condition, because we have two DVs, there is an additional bit of wrangling required to get the data in the right format for plotting.</p>
<p>In the current long-form dataset, the column <code>dv</code> contains both reaction time and accuracy measures and keeping in mind the rule of thumb that <em>anything that shares an axis should probably be in the same column,</em> this creates a problem because we cannot plot two different units of measurement on the same axis. To fix this we need to use the function <code>pivot_wider()</code>. Again, we would encourage you at this point to compare <code>long2</code> and <code>dat_long</code> with the below code to try and map the connections before reading on.</p>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="chapter-3.html#cb27-1" aria-hidden="true" tabindex="-1"></a>dat_long <span class="ot">&lt;-</span> <span class="fu">pivot_wider</span>(long2, </span>
<span id="cb27-2"><a href="chapter-3.html#cb27-2" aria-hidden="true" tabindex="-1"></a>                        <span class="at">names_from =</span> <span class="st">&quot;dv_type&quot;</span>, </span>
<span id="cb27-3"><a href="chapter-3.html#cb27-3" aria-hidden="true" tabindex="-1"></a>                        <span class="at">values_from =</span> <span class="st">&quot;dv&quot;</span>)</span></code></pre></div>
<ul>
<li><p>The first argument is again the dataset you wish to work from, in this case <code>long2</code>. We have removed the argument name <code>data</code> in this example.</p></li>
<li><p><code>names_from</code> acts somewhat like the reverse of <code>names_to</code> from <code>pivot_longer()</code>. It will take the values from the variable specified and use these as variable names, i.e., in this case, the values of <code>rt</code> and <code>acc</code> that are currently in the <code>dv_type</code> column, and turn these into the column names.</p></li>
<li><p>Finally, <code>values_from</code> specifies the values to fill the new columns with. In this case, the new columns <code>rt</code> and <code>acc</code> will be filled with the values that were in <code>dv</code>. Again, it can be helpful to compare each dataset with the code to see how it aligns.</p></li>
</ul>
<p>This final long-form data should looks like Table <a href="chapter-3.html#tab:long-example">3.4</a>.</p>
<p>If you are working with a dataset with only one DV, note that only step 1 of this process would be necessary. Also, be careful not to calculate demographic descriptive statistics from this long-form dataset. Because the process of transformation has introduced some repetition for these variables, the wide-form dataset where 1 row = 1 participant should be used for demographic information. Finally, the three step process noted above is broken down for teaching purposes, in reality, one would likely do this in a single pipeline of code, for example:</p>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb28-1"><a href="chapter-3.html#cb28-1" aria-hidden="true" tabindex="-1"></a>dat_long <span class="ot">&lt;-</span> <span class="fu">pivot_longer</span>(<span class="at">data =</span> dat, </span>
<span id="cb28-2"><a href="chapter-3.html#cb28-2" aria-hidden="true" tabindex="-1"></a>                     <span class="at">cols =</span> rt_word<span class="sc">:</span>acc_nonword, </span>
<span id="cb28-3"><a href="chapter-3.html#cb28-3" aria-hidden="true" tabindex="-1"></a>                     <span class="at">names_sep =</span> <span class="st">&quot;_&quot;</span>, </span>
<span id="cb28-4"><a href="chapter-3.html#cb28-4" aria-hidden="true" tabindex="-1"></a>                     <span class="at">names_to =</span> <span class="fu">c</span>(<span class="st">&quot;dv_type&quot;</span>, <span class="st">&quot;condition&quot;</span>),</span>
<span id="cb28-5"><a href="chapter-3.html#cb28-5" aria-hidden="true" tabindex="-1"></a>                     <span class="at">values_to =</span> <span class="st">&quot;dv&quot;</span>) <span class="sc">%&gt;%</span></span>
<span id="cb28-6"><a href="chapter-3.html#cb28-6" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_wider</span>(<span class="at">names_from =</span> <span class="st">&quot;dv_type&quot;</span>, </span>
<span id="cb28-7"><a href="chapter-3.html#cb28-7" aria-hidden="true" tabindex="-1"></a>              <span class="at">values_from =</span> <span class="st">&quot;dv&quot;</span>)</span></code></pre></div>
</div>
</div>
<div id="histogram-2" class="section level2" number="3.3">
<h2><span class="header-section-number">3.3</span> Histogram 2</h2>
<p>Now that we have the experimental data in the right form, we can begin to create some useful visualizations. First, to demonstrate how code recipes can be reused and adapted, we will create histograms of reaction time and accuracy. The below code uses the same template as before but changes the dataset (<code>dat_long</code>), the bin-widths of the histograms, the <code>x</code> variable to display (<code>rt</code>/<code>acc</code>), and the name of the x-axis.</p>
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="chapter-3.html#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt)) <span class="sc">+</span></span>
<span id="cb29-2"><a href="chapter-3.html#cb29-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_histogram</span>(<span class="at">binwidth =</span> <span class="dv">10</span>, <span class="at">fill =</span> <span class="st">&quot;white&quot;</span>, <span class="at">colour =</span> <span class="st">&quot;black&quot;</span>) <span class="sc">+</span></span>
<span id="cb29-3"><a href="chapter-3.html#cb29-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Reaction time (ms)&quot;</span>)</span>
<span id="cb29-4"><a href="chapter-3.html#cb29-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb29-5"><a href="chapter-3.html#cb29-5" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> acc)) <span class="sc">+</span></span>
<span id="cb29-6"><a href="chapter-3.html#cb29-6" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_histogram</span>(<span class="at">binwidth =</span> <span class="dv">1</span>, <span class="at">fill =</span> <span class="st">&quot;white&quot;</span>, <span class="at">colour =</span> <span class="st">&quot;black&quot;</span>) <span class="sc">+</span></span>
<span id="cb29-7"><a href="chapter-3.html#cb29-7" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Accuracy (0-100)&quot;</span>)</span></code></pre></div>
<div class="figure" style="text-align: center"><span id="fig:histograms"></span>
<img src="03-ch3_files/figure-html/histograms-1.png" alt="Histograms showing the distribution of reaction time (top) and accuracy (bottom)" width="100%" />
<p class="caption">
Figure 3.1: Histograms showing the distribution of reaction time (top) and accuracy (bottom)
</p>
</div>
</div>
<div id="density-plots" class="section level2" number="3.4">
<h2><span class="header-section-number">3.4</span> Density plots</h2>
<p>The layer system makes it easy to create new types of plots by adapting existing recipes. For example, rather than creating a histogram, we can create a smoothed density plot by calling <code>geom_density()</code> rather than <code>geom_histogram()</code>. The rest of the code remains identical.</p>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb30-1"><a href="chapter-3.html#cb30-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt)) <span class="sc">+</span></span>
<span id="cb30-2"><a href="chapter-3.html#cb30-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_density</span>()<span class="sc">+</span></span>
<span id="cb30-3"><a href="chapter-3.html#cb30-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Reaction time (ms)&quot;</span>)</span></code></pre></div>
<div class="figure" style="text-align: center"><span id="fig:density-rt"></span>
<img src="03-ch3_files/figure-html/density-rt-1.png" alt="Density plot of reaction time." width="100%" />
<p class="caption">
Figure 3.2: Density plot of reaction time.
</p>
</div>
<div id="grouped-density-plots" class="section level3" number="3.4.1">
<h3><span class="header-section-number">3.4.1</span> Grouped density plots</h3>
<p>Density plots are most useful for comparing the distributions of different groups of data. Because the dataset is now in long format, it makes it easier to map another variable to the plot because each variable is contained within a single column.</p>
<ul>
<li>In addition to mapping <code>rt</code> to the x-axis, we specify the <code>fill</code> aesthetic to fill the visualisation of each level of the <code>condition</code> variable with different colours.</li>
<li>As with the x and y-axis scale functions, we can edit the names and labels of our fill aesthetic by adding on another <code>scale_*</code> layer.</li>
<li>Note that the <code>fill</code> here is set inside the <code>aes()</code> function, which tells ggplot to set the fill differently for each value in the <code>condition</code> column. You cannot specify which colour here (e.g., <code>fill="red"</code>), like you could when you set <code>fill</code> inside the <code>geom_*()</code> function before.</li>
</ul>
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="chapter-3.html#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt, <span class="at">fill =</span> condition)) <span class="sc">+</span></span>
<span id="cb31-2"><a href="chapter-3.html#cb31-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_density</span>()<span class="sc">+</span></span>
<span id="cb31-3"><a href="chapter-3.html#cb31-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Reaction time (ms)&quot;</span>) <span class="sc">+</span></span>
<span id="cb31-4"><a href="chapter-3.html#cb31-4" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_fill_discrete</span>(<span class="at">name =</span> <span class="st">&quot;Condition&quot;</span>,</span>
<span id="cb31-5"><a href="chapter-3.html#cb31-5" aria-hidden="true" tabindex="-1"></a>                      <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">&quot;Word&quot;</span>, <span class="st">&quot;Non-word&quot;</span>))</span></code></pre></div>
<div class="figure" style="text-align: center"><span id="fig:density-grouped"></span>
<img src="03-ch3_files/figure-html/density-grouped-1.png" alt="Density plot of reaction times grouped by condition." width="100%" />
<p class="caption">
Figure 3.3: Density plot of reaction times grouped by condition.
</p>
</div>
</div>
</div>
<div id="scatterplots" class="section level2" number="3.5">
<h2><span class="header-section-number">3.5</span> Scatterplots</h2>
<p>Scatterplots are created by calling <code>geom_point()</code> and require both an <code>x</code> and <code>y</code> variable to be specified in the mapping.</p>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="chapter-3.html#cb32-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt, <span class="at">y =</span> age)) <span class="sc">+</span></span>
<span id="cb32-2"><a href="chapter-3.html#cb32-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>()</span></code></pre></div>
<div class="figure" style="text-align: center"><span id="fig:point-plot"></span>
<img src="03-ch3_files/figure-html/point-plot-1.png" alt="Point plot of reaction time versus age." width="100%" />
<p class="caption">
Figure 3.4: Point plot of reaction time versus age.
</p>
</div>
<p>A line of best fit can be added with an additional layer that calls the function <code>geom_smooth()</code>. The default is to draw a LOESS or curved regression line, however, a linear line of best fit can be specified using <code>method = "lm"</code>. By default, <code>geom_smooth()</code> will also draw a confidence envelope around the regression line, this can be removed by adding <code>se = FALSE</code> to <code>geom_smooth()</code>. A common error is to try and use <code>geom_line()</code> to draw the line of best fit, which whilst a sensible guess, will not work (try it).</p>
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="chapter-3.html#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt, <span class="at">y =</span> age)) <span class="sc">+</span></span>
<span id="cb33-2"><a href="chapter-3.html#cb33-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb33-3"><a href="chapter-3.html#cb33-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_smooth</span>(<span class="at">method =</span> <span class="st">&quot;lm&quot;</span>)</span></code></pre></div>
<div class="figure" style="text-align: center"><span id="fig:smooth-plot"></span>
<img src="03-ch3_files/figure-html/smooth-plot-1.png" alt="Line of best fit for reaction time versus age." width="100%" />
<p class="caption">
Figure 3.5: Line of best fit for reaction time versus age.
</p>
</div>
<div id="grouped-scatterplots" class="section level3" number="3.5.1">
<h3><span class="header-section-number">3.5.1</span> Grouped scatterplots</h3>
<p>Similar to the density plot, the scatterplot can also be easily adjusted to display grouped data. For <code>geom_point()</code>, the grouping variable is mapped to <code>colour</code> rather than <code>fill</code> and the relevant <code>scale_</code> function is added.</p>
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb34-1"><a href="chapter-3.html#cb34-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt, <span class="at">y =</span> age, <span class="at">colour =</span> condition)) <span class="sc">+</span></span>
<span id="cb34-2"><a href="chapter-3.html#cb34-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb34-3"><a href="chapter-3.html#cb34-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_smooth</span>(<span class="at">method =</span> <span class="st">&quot;lm&quot;</span>) <span class="sc">+</span></span>
<span id="cb34-4"><a href="chapter-3.html#cb34-4" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_colour_discrete</span>(<span class="at">name =</span> <span class="st">&quot;Condition&quot;</span>,</span>
<span id="cb34-5"><a href="chapter-3.html#cb34-5" aria-hidden="true" tabindex="-1"></a>                      <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">&quot;Word&quot;</span>, <span class="st">&quot;Non-word&quot;</span>))</span></code></pre></div>
<div class="figure" style="text-align: center"><span id="fig:scatter-grouped"></span>
<img src="03-ch3_files/figure-html/scatter-grouped-1.png" alt="Grouped scatter plot of reaction time versus age by condition." width="100%" />
<p class="caption">
Figure 3.6: Grouped scatter plot of reaction time versus age by condition.
</p>
</div>
</div>
</div>
<div id="transforming-data-2" class="section level2" number="3.6">
<h2><span class="header-section-number">3.6</span> Transforming data 2</h2>
<p>Following the rule that <em>anything that shares an axis should probably be in the same column</em> means that we will frequently need our data in long-form when using <code>ggplot2</code>, however, there are some cases when wide-form is necessary. For example, we may wish to visualise the relationship between reaction time in the word and non-word conditions. The easiest way to achieve this in our case would simply be to use the original wide-form data as the input:</p>
<div class="sourceCode" id="cb35"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb35-1"><a href="chapter-3.html#cb35-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat, <span class="fu">aes</span>(<span class="at">x =</span> rt_word, <span class="at">y =</span> rt_nonword, <span class="at">colour =</span> language)) <span class="sc">+</span></span>
<span id="cb35-2"><a href="chapter-3.html#cb35-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb35-3"><a href="chapter-3.html#cb35-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_smooth</span>(<span class="at">method =</span> <span class="st">&quot;lm&quot;</span>)</span></code></pre></div>
<div class="figure" style="text-align: center"><span id="fig:unnamed-chunk-6"></span>
<img src="03-ch3_files/figure-html/unnamed-chunk-6-1.png" alt="Scatterplot with data grouped by langauge group" width="100%" />
<p class="caption">
Figure 3.7: Scatterplot with data grouped by langauge group
</p>
</div>
<p>However, there may also be cases when you do not have an original wide-form version and you can use the <code>pivot_wider()</code> function to transform from long to wide.</p>
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb36-1"><a href="chapter-3.html#cb36-1" aria-hidden="true" tabindex="-1"></a>dat_wide <span class="ot">&lt;-</span> dat_long <span class="sc">%&gt;%</span></span>
<span id="cb36-2"><a href="chapter-3.html#cb36-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">pivot_wider</span>(<span class="at">id_cols =</span> <span class="st">&quot;id&quot;</span>,</span>
<span id="cb36-3"><a href="chapter-3.html#cb36-3" aria-hidden="true" tabindex="-1"></a>              <span class="at">names_from =</span> <span class="st">&quot;condition&quot;</span>, </span>
<span id="cb36-4"><a href="chapter-3.html#cb36-4" aria-hidden="true" tabindex="-1"></a>              <span class="at">values_from =</span> <span class="fu">c</span>(rt,acc))</span></code></pre></div>
<table>
<thead>
<tr class="header">
<th align="center">id</th>
<th align="center">rt_word</th>
<th align="center">rt_nonword</th>
<th align="center">acc_word</th>
<th align="center">acc_nonword</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="center">S001</td>
<td align="center">379.4585</td>
<td align="center">516.8176</td>
<td align="center">99</td>
<td align="center">90</td>
</tr>
<tr class="even">
<td align="center">S002</td>
<td align="center">312.4513</td>
<td align="center">435.0404</td>
<td align="center">94</td>
<td align="center">82</td>
</tr>
<tr class="odd">
<td align="center">S003</td>
<td align="center">404.9407</td>
<td align="center">458.5022</td>
<td align="center">96</td>
<td align="center">87</td>
</tr>
<tr class="even">
<td align="center">S004</td>
<td align="center">298.3734</td>
<td align="center">335.8933</td>
<td align="center">92</td>
<td align="center">76</td>
</tr>
<tr class="odd">
<td align="center">S005</td>
<td align="center">316.4250</td>
<td align="center">401.3214</td>
<td align="center">91</td>
<td align="center">83</td>
</tr>
<tr class="even">
<td align="center">S006</td>
<td align="center">357.1710</td>
<td align="center">367.3355</td>
<td align="center">96</td>
<td align="center">78</td>
</tr>
</tbody>
</table>
</div>
<div id="customisation-2" class="section level2" number="3.7">
<h2><span class="header-section-number">3.7</span> Customisation 2</h2>
<div id="accessible-colour-schemes" class="section level3" number="3.7.1">
<h3><span class="header-section-number">3.7.1</span> Accessible colour schemes</h3>
<p>One of the drawbacks of using <code>ggplot</code> for visualisation is that the default colour scheme is not accessible (or visually appealing). The red and green default palette is difficult for colour-blind people to differentiate, and also does not display well in grey scale. You can specify exact custom colours for your plots, but one easy option is to use a colour palette and the <code>viridis</code> scale functions call such a palette. These take the same arguments as their default <code>scale</code> sister functions for updating axis names and labels, but display plots in contrasting colours that can be read by colour-blind people and that also print well in grey scale. The <code>viridis</code> scale functions provide a number of different options for the colour - try setting <code>option</code> to any letter from A - E to see the different sets.</p>
<div class="sourceCode" id="cb37"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb37-1"><a href="chapter-3.html#cb37-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt, <span class="at">y =</span> age, <span class="at">colour =</span> condition)) <span class="sc">+</span></span>
<span id="cb37-2"><a href="chapter-3.html#cb37-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb37-3"><a href="chapter-3.html#cb37-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_smooth</span>(<span class="at">method =</span> <span class="st">&quot;lm&quot;</span>) <span class="sc">+</span></span>
<span id="cb37-4"><a href="chapter-3.html#cb37-4" aria-hidden="true" tabindex="-1"></a>  <span class="co"># use &quot;viridis_d&quot; instead of &quot;discrete&quot; for better colours</span></span>
<span id="cb37-5"><a href="chapter-3.html#cb37-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_colour_viridis_d</span>(<span class="at">name =</span> <span class="st">&quot;Condition&quot;</span>,</span>
<span id="cb37-6"><a href="chapter-3.html#cb37-6" aria-hidden="true" tabindex="-1"></a>                        <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">&quot;Word&quot;</span>, <span class="st">&quot;Non-word&quot;</span>),</span>
<span id="cb37-7"><a href="chapter-3.html#cb37-7" aria-hidden="true" tabindex="-1"></a>                        <span class="at">option =</span> <span class="st">&quot;E&quot;</span>)</span></code></pre></div>
<div class="figure" style="text-align: center"><span id="fig:viridis"></span>
<img src="03-ch3_files/figure-html/viridis-1.png" alt="Use the viridis colour scheme for accessibility." width="100%" />
<p class="caption">
Figure 3.8: Use the viridis colour scheme for accessibility.
</p>
</div>
</div>
</div>
<div id="activities-2" class="section level2" number="3.8">
<h2><span class="header-section-number">3.8</span> Activities 2</h2>
<p>Before you move on try the following:</p>
<ol style="list-style-type: decimal">
<li>Use <code>fill</code> to created grouped histograms that display the distributions for <code>rt</code> for each <code>language</code> group separately and also edit the fill axis labels. Try adding <code>position = "dodge"</code> to <code>geom_histogram()</code> to see what happens.</li>
</ol>
<div class="webex-solution">
<button>
Solution 1
</button>
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb38-1"><a href="chapter-3.html#cb38-1" aria-hidden="true" tabindex="-1"></a><span class="co"># fill and axis changes</span></span>
<span id="cb38-2"><a href="chapter-3.html#cb38-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt, <span class="at">fill =</span> language)) <span class="sc">+</span></span>
<span id="cb38-3"><a href="chapter-3.html#cb38-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_histogram</span>(<span class="at">binwidth =</span> <span class="dv">10</span>) <span class="sc">+</span></span>
<span id="cb38-4"><a href="chapter-3.html#cb38-4" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Reaction time (ms)&quot;</span>) <span class="sc">+</span></span>
<span id="cb38-5"><a href="chapter-3.html#cb38-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_fill_discrete</span>(<span class="at">name =</span> <span class="st">&quot;Group&quot;</span>,</span>
<span id="cb38-6"><a href="chapter-3.html#cb38-6" aria-hidden="true" tabindex="-1"></a>                      <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">&quot;Monolingual&quot;</span>, <span class="st">&quot;Bilingual&quot;</span>))</span>
<span id="cb38-7"><a href="chapter-3.html#cb38-7" aria-hidden="true" tabindex="-1"></a><span class="co"># add in dodge</span></span>
<span id="cb38-8"><a href="chapter-3.html#cb38-8" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt, <span class="at">fill =</span> language)) <span class="sc">+</span></span>
<span id="cb38-9"><a href="chapter-3.html#cb38-9" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_histogram</span>(<span class="at">binwidth =</span> <span class="dv">10</span>, <span class="at">position =</span> <span class="st">&quot;dodge&quot;</span>) <span class="sc">+</span></span>
<span id="cb38-10"><a href="chapter-3.html#cb38-10" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Reaction time (ms)&quot;</span>) <span class="sc">+</span></span>
<span id="cb38-11"><a href="chapter-3.html#cb38-11" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_fill_discrete</span>(<span class="at">name =</span> <span class="st">&quot;Group&quot;</span>,</span>
<span id="cb38-12"><a href="chapter-3.html#cb38-12" aria-hidden="true" tabindex="-1"></a>                      <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">&quot;Monolingual&quot;</span>, <span class="st">&quot;Bilingual&quot;</span>))</span></code></pre></div>
</div>
<ol start="2" style="list-style-type: decimal">
<li>Use <code>scale_*_*()</code> functions to edit the name of the x and y-axis on the scatterplot</li>
</ol>
<div class="webex-solution">
<button>
Solution 2
</button>
<div class="sourceCode" id="cb39"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb39-1"><a href="chapter-3.html#cb39-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt, <span class="at">y =</span> age)) <span class="sc">+</span></span>
<span id="cb39-2"><a href="chapter-3.html#cb39-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb39-3"><a href="chapter-3.html#cb39-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_smooth</span>(<span class="at">method =</span> <span class="st">&quot;lm&quot;</span>) <span class="sc">+</span></span>
<span id="cb39-4"><a href="chapter-3.html#cb39-4" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Reaction time&quot;</span>) <span class="sc">+</span></span>
<span id="cb39-5"><a href="chapter-3.html#cb39-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_y_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Age&quot;</span>)</span></code></pre></div>
</div>
<ol start="3" style="list-style-type: decimal">
<li>Use <code>se = FALSE</code> to remove the confidence envelope from the scatterplots</li>
</ol>
<div class='webex-solution'>
<button>
Solution 3
</button>
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb40-1"><a href="chapter-3.html#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt, <span class="at">y =</span> age)) <span class="sc">+</span></span>
<span id="cb40-2"><a href="chapter-3.html#cb40-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb40-3"><a href="chapter-3.html#cb40-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_smooth</span>(<span class="at">method =</span> <span class="st">&quot;lm&quot;</span>, <span class="at">se =</span> <span class="cn">FALSE</span>) <span class="sc">+</span></span>
<span id="cb40-4"><a href="chapter-3.html#cb40-4" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Reaction time&quot;</span>) <span class="sc">+</span></span>
<span id="cb40-5"><a href="chapter-3.html#cb40-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_y_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Age&quot;</span>)</span></code></pre></div>
<ol start="4" style="list-style-type: decimal">
<li>Remove <code>method = "lm"</code> from <code>geom_smooth()</code> to produce a curved regression line.</li>
</ol>
<div class='webex-solution'>
<button>
Solution 4
</button>
<div class="sourceCode" id="cb41"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb41-1"><a href="chapter-3.html#cb41-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt, <span class="at">y =</span> age)) <span class="sc">+</span></span>
<span id="cb41-2"><a href="chapter-3.html#cb41-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb41-3"><a href="chapter-3.html#cb41-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_smooth</span>() <span class="sc">+</span></span>
<span id="cb41-4"><a href="chapter-3.html#cb41-4" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Reaction time&quot;</span>) <span class="sc">+</span></span>
<span id="cb41-5"><a href="chapter-3.html#cb41-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_y_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Age&quot;</span>)</span></code></pre></div>
<ol start="5" style="list-style-type: decimal">
<li>Replace the default <code>scale_fill_*()</code> on the grouped density plot with the colour-blind friendly version.</li>
</ol>
<div class='webex-solution'>
<button>
Solution
</button>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb42-1"><a href="chapter-3.html#cb42-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(dat_long, <span class="fu">aes</span>(<span class="at">x =</span> rt, <span class="at">fill =</span> condition)) <span class="sc">+</span></span>
<span id="cb42-2"><a href="chapter-3.html#cb42-2" aria-hidden="true" tabindex="-1"></a>  <span class="fu">geom_density</span>()<span class="sc">+</span></span>
<span id="cb42-3"><a href="chapter-3.html#cb42-3" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_x_continuous</span>(<span class="at">name =</span> <span class="st">&quot;Reaction time (ms)&quot;</span>) <span class="sc">+</span></span>
<span id="cb42-4"><a href="chapter-3.html#cb42-4" aria-hidden="true" tabindex="-1"></a>  <span class="fu">scale_fill_viridis_d</span>(<span class="at">option =</span> <span class="st">&quot;E&quot;</span>,</span>
<span id="cb42-5"><a href="chapter-3.html#cb42-5" aria-hidden="true" tabindex="-1"></a>                      <span class="at">name =</span> <span class="st">&quot;Condition&quot;</span>,</span>
<span id="cb42-6"><a href="chapter-3.html#cb42-6" aria-hidden="true" tabindex="-1"></a>                      <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">&quot;Word&quot;</span>, <span class="st">&quot;Non-word&quot;</span>))</span></code></pre></div>

</div>
</div>
<h3>References</h3>
<div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-wickham2014tidy" class="csl-entry">
Wickham, Hadley, and others. 2014. <span>“Tidy Data.”</span> <em>Journal of Statistical Software</em> 59 (10): 1–23.
</div>
</div>
<div class="footnotes">
<hr />
<ol start="3">
<li id="fn3"><p>In this tutorial we have chosen to gloss over the data processing steps that must occur to get from the raw data to aggregated values. This type of processing requires a more extensive tutorial than we can provide in the current paper. More importantly, it is still possible to use R for data visualisation having done the preparatory steps using existing workflows in Excel and SPSS, so long as the file is saved/exported as a <code>.csv</code> file. We bypass these initial steps and focus on tangible outputs that may then encourage further mastery of reproducible methods. Collectively we tend to call the steps for reshaping data and for processing raw data or for getting data ready to use statistical functions “wrangling.”<a href="chapter-3.html#fnref3" class="footnote-back">↩︎</a></p></li>
<li id="fn4"><p>That is to say, if you are new to R, know that many before you have struggled with this conceptual shift - it does get better, it just takes time and your preferred choice of cursing.<a href="chapter-3.html#fnref4" class="footnote-back">↩︎</a></p></li>
</ol>
</div>
<div class="psyteachr_footer">
  
</div>
<script>

/* update total correct if #webex-total_correct exists */
update_total_correct = function() {
  if (t = document.getElementById("webex-total_correct")) {
    t.innerHTML =
      document.getElementsByClassName("webex-correct").length + " of " +
      document.getElementsByClassName("webex-solveme").length + " correct";
  }
}

/* webex-solution button toggling function */
b_func = function() {
  var cl = this.parentElement.classList;
  if (cl.contains('open')) {
    cl.remove("open");
  } else {
    cl.add("open");
  }
}

/* function for checking solveme answers */
solveme_func = function(e) {
  var real_answers = JSON.parse(this.dataset.answer);
  var my_answer = this.value;
  var cl = this.classList;
  if (cl.contains("ignorecase")) {
    my_answer = my_answer.toLowerCase();
  }
  if (cl.contains("nospaces")) {
    my_answer = my_answer.replace(/ /g, "")
  }
  
  if (my_answer !== "" & real_answers.includes(my_answer)) {
    cl.add("webex-correct");
  } else {
    cl.remove("webex-correct");
  }

  // match numeric answers within a specified tolerance
  if(this.dataset.tol > 0){
    var tol = JSON.parse(this.dataset.tol);  
    var matches = real_answers.map(x => Math.abs(x - my_answer) < tol)
    if (matches.reduce((a, b) => a + b, 0) > 0) {
      cl.add("webex-correct");
    } else {
      cl.remove("webex-correct");
    }  
  }

  // added regex bit
  if (cl.contains("regex")){
    answer_regex = RegExp(real_answers.join("|"))
    if (answer_regex.test(my_answer)) {
      cl.add("webex-correct");
    }  
  }
  
  update_total_correct();
}

window.onload = function() {
  /* set up solution buttons */
  var buttons = document.getElementsByTagName("button");

  for (var i = 0; i < buttons.length; i++) {
    if (buttons[i].parentElement.classList.contains('webex-solution')) {
      buttons[i].onclick = b_func;
    }
  }
  
  /* set up webex-solveme inputs */
  var solveme = document.getElementsByClassName("webex-solveme");

  for (var i = 0; i < solveme.length; i++) {
    /* make sure input boxes don't auto-anything */
    solveme[i].setAttribute("autocomplete","off");
    solveme[i].setAttribute("autocorrect", "off");
    solveme[i].setAttribute("autocapitalize", "off"); 
    solveme[i].setAttribute("spellcheck", "false");
    solveme[i].value = "";
    
    /* adjust answer for ignorecase or nospaces */
    var cl = solveme[i].classList;
    var real_answer = solveme[i].dataset.answer;
    if (cl.contains("ignorecase")) {
      real_answer = real_answer.toLowerCase();
    }
    if (cl.contains("nospaces")) {
      real_answer = real_answer.replace(/ /g, "");
    }
    solveme[i].dataset.answer = real_answer;
    
    /* attach checking function */
    solveme[i].onkeyup = solveme_func;
    solveme[i].onchange = solveme_func;
  }
  
  update_total_correct();
}

</script>
            </section>

          </div>
        </div>
      </div>
<a href="chapter-2.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="chapter-4.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "google", "twitter", "linkedin", "weibo", "instapaper"],
"google": false
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": {},
"toc": {
"collapse": "section",
"scroll_highlight": true
}
});
});
</script>

</body>

</html>
